<template>
  <div style="height: 100%; width: 100%">
    <div v-show="customerId">
      <configTable
        ref="table"
        style="width: 100%"
        :headers="tableHeaders"
        :data="tableData"
        :pagingData="pagingData"
        @pageChange="handleCurrentChange"
        @pageSizeChange="handleSizeChange"
        @checkHeadersChange="checkHeadersChange"
      >
        <template slot="column">
          <template v-for="(item, index) in checkHeaders">
            <u-table-column
              v-if="
                item.show &&
                !['oprate', 'isDefault', 'phone'].includes(item.prop)
              "
              :key="index"
              :label="item.label"
              :prop="item.prop"
              :sortable="item.sortable"
              :min-width="item.width || 80"
            >
            </u-table-column>
            <u-table-column
              v-if="['isDefault'].includes(item.prop) && item.show"
              :key="index"
              :label="item.label"
              :prop="item.prop"
              :sortable="item.sortable"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.isDefault | getIsDefaultName }}</span>
              </template>
            </u-table-column>
            <u-table-column
              v-if="['phone'].includes(item.prop) && item.show"
              :key="index"
              :label="item.label"
              :prop="item.prop"
              :sortable="item.sortable"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.phone }}</span>
              </template>
            </u-table-column>
            <u-table-column
              v-if="item.show && item.prop === 'oprate'"
              :key="index"
              :label="item.label"
              :resizable="false"
              :width="60"
              fixed="right"
            >
              <template slot-scope="scope">
                <span class="oprate-span" @click="edit(scope.row)">编辑</span>
                <span class="oprate-span red-span" @click="remove(scope.row)"
                  >删除</span
                >
              </template>
            </u-table-column>
          </template>
        </template>
      </configTable>
      <Persion
        :noBtn="true"
        ref="Persion"
        :customerId="customerId"
        @updatePerson="updatePerson"
      ></Persion>
    </div>
    <div class="no-data" v-show="!customerId"></div>
  </div>
</template>

<script lang="ts">
/**
 * 客户详情联系人组件
 */
import { Component, Vue, Ref, Prop } from 'vue-property-decorator'
import configTable from '@/components/globalTool/resizeTable/index.vue'
import Persion from './buttons/person.vue'
import { commonAction, checkHideTel } from '@/utils/common'
@Component({
  name: 'ContactPerson',
  components: {
    configTable,
    Persion
  },
  filters: {
    getIsDefaultName (type) {
      let typeName = '--'
      if (type === 1) {
        typeName = '是'
      } else if (type === 0) {
        typeName = '否'
      }
      return typeName
    }
    // getHideTel (customerNumber) {
    //   return checkHideTel(customerNumber)
    // }
  }
})
export default class ContactPerson extends Vue {
  [x: string]: any
  @Ref() private readonly Persion: any
  @Prop() private customerId
  private tableData: any = []

  private pagingData: any = { total: 0, limit: 10, page: 1 }

  private checkHeaders: [] = []

  public getData () {
    const sendData = {
      limit: this.pagingData.limit,
      page: this.pagingData.page,
      customerId: this.customerId
    }
    const params = { url: '/server-crm/action/contactor/list', method: 'POST', data: sendData }
    commonAction(params).then(res => {
      if (res.success && res.data) {
        res.data.list.map(item => {
          item.realPhone = item.phone
          item.phone = checkHideTel(item.phone)
        })
        this.tableData = res.data.list
        this.pagingData.total = res.data.count
      }
    })
  }

  private handleSearchFunc () {
    this.pagingData.page = 1
    this.getData()
  }

  private updatePerson () {
    this.getData()
  }

  private tableHeaders: object[] = [
    { label: '联系人姓名', prop: 'contactName' },
    { label: '手机号码', prop: 'phone' },
    { label: '邮箱', prop: 'contactEmail' },
    { label: '默认联系人', prop: 'isDefault' },
    { label: '操作', prop: 'oprate', disenable: true }
  ]

  private checkHeadersChange (checkList) {
    this.$set(this, 'checkHeaders', checkList)
  }

  private handleSizeChange (size: number) {
    this.pagingData.limit = size
    this.handleSearchFunc()
  }

  private handleCurrentChange (page: number) {
    this.pagingData.page = page
    this.getData()
  }

  private edit (item) {
    this.Persion.openDialog(item, 'edit')
  }

  private remove (row: any) {
    this.$confirm('请确认是否删除该联系人？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(async () => {
      const sendData = { id: row._id, userId: this.customerId }
      const params = { url: '/server-crm/action/contactor/delete', method: 'POST', data: sendData }
      commonAction(params).then(res => {
        if (res.success) {
          this.getData()
          this.$message.success('删除成功')
        }
      })
    })
  }
}
</script>

<style lang="stylus" scoped>
.no-data
  background url('../../../assets/img/no-data.png') no-repeat center center
  width 100%
  height 200px
  background-size 267px auto
.oprate-span
  cursor pointer
  color $c-main
  margin 0 4px
.red-span, .disable-span
  color $c-danger
</style>
